package com.zzmm.socket.qq.client;

import com.zzmm.socket.qq.common.ConsoleUtil;
import lombok.SneakyThrows;

public class QQClient {

    public static void main(String[] args) {
        new QQClient().view();
    }

    // 客户端启停开关
    private boolean loop = true;
    // 客户端菜单层级
    private int level = 1;
    // 用户选择的菜单
    private String select;

    // 用户服务
    private final ClientService clientService = new ClientService();


    public void view() {

        do {
            // 一级菜单
            levelOne();

            // 二级菜单
            levelTwo();


        } while (loop);

    }

    /**
     * 二级菜单
     */
    @SuppressWarnings("all")
    @SneakyThrows
    private void levelTwo() {
        while (loop && level == 2) {
            ConsoleUtil.printf("==================网络通信系统二级菜单（用户 %s）==================\n", clientService.getUserName());
            ConsoleUtil.println("\t\t1 显示在线用户列表");
            ConsoleUtil.println("\t\t2 群发消息");
            ConsoleUtil.println("\t\t3 私聊消息");
            ConsoleUtil.println("\t\t4 发送文件");
            ConsoleUtil.println("\t\t8 刷新");
            ConsoleUtil.println("\t\t9 退出系统");

            select = ConsoleUtil.getString("请输入你的选择：");
            switch (select) {
                case "1":
                    clientService.listOnlineUser();
                    break;
                case "2":
                    clientService.groupChat();
                    break;
                case "3":
                    clientService.privateChat();
                    break;
                case "4":
                    clientService.sendFile();
                    break;
                case "8":
                    Thread.sleep(1000); // 这里睡一秒，让其他线程在控制台打印或接收数据
                    break;
                case "9":
                    exit();
                    break;
                default:
                    selectErrMsg();
            }
        }
    }

    /**
     * 一级菜单
     */
    private void levelOne() {
        do {
            ConsoleUtil.println("==================欢迎登录网络通信系统==================");
            ConsoleUtil.println("\t\t1 登录系统");
            ConsoleUtil.println("\t\t9 退出系统");

            select = ConsoleUtil.getString("请输入你的选择：");
            switch (select) {
                case "1":
                    // 登录
                    if (clientService.login()) {
                        ConsoleUtil.printf("==================欢迎用户【%s】==================\n", clientService.getUserName());
                        level = 2;
                    } else {
                        ConsoleUtil.println("QQServer：用户号或密码错误");
                    }
                    break;
                case "9":
                    exit();
                    break;
                default:
                    selectErrMsg();
            }
        } while (loop && level == 1);
    }

    /**
     * 输入错误消息
     */
    private void selectErrMsg() {
        ConsoleUtil.println("选择错误，请重新输入");
    }

    /**
     * 退出
     */
    private void exit() {
        loop = false;
        clientService.exit();
        ConsoleUtil.println("客户端退出系统...");
    }

}
